package Lianxi.ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 转账功能
 */
public class Transfer extends JPanel implements ActionListener {
    private JButton returnBut; //返回
    private JButton confirm;  //确认
    private JTextField tarnMoney;//转账金额
    private JTextField tarnId;
    private Atm frame;
    private int sum = 0;

    public Transfer(Atm frame) {

        this.frame = frame;

        this.setLayout(null); //空布局

        this.setOpaque(false); //背景透明


        returnBut = new JButton("返回");
        returnBut.setBounds(350, 580, 100, 50);
        this.add(returnBut);
        returnBut.addActionListener(this);

        //确认按钮,确认取款金额
        confirm = new JButton("确认");
        confirm.setBounds(350, 500, 100, 50);
        this.add(confirm);
        confirm.addActionListener(this);

        tarnMoney = new JTextField(); //转账金额文本框
        tarnMoney.setBounds(320, 220, 100, 30);
        this.add(tarnMoney);
        tarnMoney.addActionListener(this);

        tarnId = new JTextField(); //转账户名文本框
        tarnId.setBounds(320, 260, 100, 30);
        this.add(tarnId);
        tarnId.addActionListener(this);


        JLabel label = new JLabel("转入金额：");
        label.setBounds(260, 220, 80, 30);
        this.add(label);

        JLabel label1 = new JLabel("转入账户：");
        label1.setBounds(260, 260, 80, 30);
        this.add(label1);


    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == returnBut) {
            frame.switchPanel("菜单");


        }

        if (e.getSource() == confirm) {  //转账
            String oth = tarnId.getText(); //获取转入账户
            int money = 0;


            try {
                money = Integer.valueOf(tarnMoney.getText());
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "转账金额必须为数字，并不能为空");
                return;

            }
            AtmSql sql = new AtmSql();
            User loginUser = sql.getById(frame.getLoginUser().getId());
            User otherUser = sql.getById(oth);

            //判断账户是否存在
            if (otherUser == null) {
                JOptionPane.showMessageDialog(this, "查无此人");
                return;
            }
            //不能给自己转账
            if (loginUser.getId().equals(otherUser.getId())) {
                JOptionPane.showMessageDialog(this, "不能转给自己");
                return;
            }
            //判断余额是否足够
            if (money > loginUser.getMoney()) {
                JOptionPane.showMessageDialog(this, "余额不足");
                return;
            }
            //当期账户扣钱
            loginUser.setMoney(loginUser.getMoney() - money);
            //对方账户加钱
            otherUser.setMoney(otherUser.getMoney() + money);
            sql.update(loginUser, otherUser); //更新数据库

            JOptionPane.showMessageDialog(this, "转账成功");
            frame.switchPanel("菜单");

        }


    }
}










